package com.guys8.cemsbackend.controller;

import com.guys8.cemsbackend.DTO.NacidInformation;
import com.guys8.cemsbackend.DTO.Response;
import com.guys8.cemsbackend.DTO.UserInformation;
import com.guys8.cemsbackend.entity.Nacid;
import com.guys8.cemsbackend.entity.User;
import com.guys8.cemsbackend.service.impl.NacidServiceImpl;
import com.guys8.cemsbackend.service.impl.UserServiceImpl;
import com.guys8.cemsbackend.utils.JWTutil.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@RestController
@Api(tags="Nacid-核酸信息相关接口")
@RequestMapping("/Nacid")
public class NacidController {

    @Autowired
    private NacidServiceImpl nacidService;
    @Autowired
    private UserServiceImpl userService;


    @PostMapping("/makeNacid/{nacidPlace}&&{nacidResult}")
    @ApiOperation("申报一个核酸状况(token)")
    public Response makeNacid(@RequestHeader String schoolid, @PathVariable String nacidPlace, @PathVariable String nacidResult) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Nacid nacid=new Nacid(user.getUserId(),nacidPlace,nacidResult);

        nacidService.save(nacid);
        Nacid NNacid = nacidService.getusernowNacid(nacid.getUserId());
        user.setLastNacid(NNacid.getNacidId());
        userService.updateById(user);
        return Response.restResult(1);
    }


    @DeleteMapping("/deleteNacid/")
    @ApiOperation("删除用户最近上传的一个核酸记录(token)")
    public Response deleteNacid(@RequestHeader String schoolid) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Nacid nacid=nacidService.getById(user.getLastNacid());
        if (nacid != null) {
            nacidService.removeById(nacid.getNacidId());
            return Response.restResult(1);
        } else {
            return Response.restResult(0);
        }

    }

    @PostMapping("/updateNacid/{nacidPlace}&&{nacidResult}")
    @ApiOperation("修改一个核酸记录(token)")
    public Response updateNacid(@RequestHeader String schoolid, @PathVariable String nacidPlace, @PathVariable String nacidResult) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Nacid nacid=nacidService.getById(user.getLastNacid());
        nacid.setNacidTime(new Date());
        nacid.setNacidPlace(nacidPlace);
        nacid.setNacidResult(nacidResult);
        nacidService.updateById(nacid);
        return Response.restResult(1);
    }

    @GetMapping("/getBySchoolid/")
    @ApiOperation("按token查找用户最近一次核酸信息(token)")
    public Response getInfoBytoken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getInfoByschoolId(schoolid);
    }

    @PostMapping("/getBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolid查找用户最近一次核酸信息")
    public Response getInfoByschoolId(@PathVariable String schoolid) {
        User user = userService.getbyschoolid(schoolid);
        if(user==null||user.getLastNacid()==null) return Response.restResult(0);
        Nacid nacid=nacidService.getById(user.getLastNacid());
        if(nacid == null) return Response.restResult(0);
        UserInformation userIn = new UserInformation(user);
        return Response.restResult(1,userIn,new NacidInformation(user,nacid));
    }

    @GetMapping("/getAllBySchoolid/")
    @ApiOperation("按token查找用户上传的所有核酸信息(token)")
    public Response getAllBytoken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getAllByschoolId(schoolid);
    }

    @GetMapping("/getAllBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolid查找用户上传的所有核酸信息")
    public Response getAllByschoolId(@PathVariable String schoolid) {
        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        List<Nacid> nacids = nacidService.getallById(user.getUserId());
        List<NacidInformation> nacidss = new LinkedList<>();
        for(Nacid n:nacids){
            nacidss.add(new NacidInformation(user,n));
        }
        UserInformation userIn = new UserInformation(user);
        return Response.restResult(1,userIn,nacidss);
    }

    @GetMapping("/getById/{NacidId}")
    @ApiOperation("//按Id查找信息(数据库测试展示接口)")
    public NacidInformation getoneById(@PathVariable Integer NacidId) {
        Nacid nacid = nacidService.getById(NacidId);
        User user = userService.getById(nacid.getUserId());
        return new NacidInformation(user,nacid);
    }
}

